package com.jiankangjin.pay.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jiankangjin.common.json.jackson.JSONUtil;
import com.jiankangjin.encryptKit.EncryptUtil;
import com.jiankangjin.encryptKit.SignUtil;
import com.jiankangjin.pay.object.Merchant;
import com.jiankangjin.pay.services.MerchantServices;
import com.jiankangjin.pay.util.StringUtil;
import com.jiankangjin.httpclient.HttpClient;
/**
 * web网关支付
 * @author fengqichao
 *
 */
@Controller
@RequestMapping("/web")
public class WebPayController {
	private static final Logger logger = Logger.getLogger(WebPayController.class);
	@Autowired
	private MerchantServices merchantServices;
	private static final String SYSTEMNAME = "[WEB支付网关]";
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "pay")
    public ModelAndView pay( 
    		@RequestParam(required = false) String input_charset,
            @RequestParam(required = false) String version,
            @RequestParam(required = false) String language,
            @RequestParam(required = false) String sign_type,
            @RequestParam(required = false) String sign,
            @RequestParam(required = false) String mer_id,
            @RequestParam(required = false) String user_id,
            @RequestParam(required = false) String page_url,
            @RequestParam(required = false) String bg_url,
            @RequestParam(required = false) String out_order_id,
            @RequestParam(required = false) String order_amount,
            @RequestParam(required = false) String goods_details,
            @RequestParam(required = false) String diag_details,
            @RequestParam(required = false) String ext1,
            @RequestParam(required = false) String ext2,
            @RequestParam(required = false) String pay_type,
            @RequestParam(required = false) String pay_channel_id){
        logger.info(SYSTEMNAME+"请求支付开始");
        //组装验签参数
        Map<String, String> map = new HashMap<String, String>();
        map.put("input_charset", input_charset);
        map.put("version", version);
        map.put("language", language);
        map.put("sign_type", sign_type);
        map.put("mer_id", mer_id);
        map.put("user_id", user_id);
        map.put("page_url", page_url);
        map.put("bg_url", bg_url);
        map.put("out_order_id", out_order_id);
        map.put("order_amount", order_amount);
        map.put("goods_details", goods_details);
        map.put("diag_details", diag_details);
        map.put("ext1", ext1);
        map.put("ext2", ext2);
        map.put("pay_type", pay_type);
        map.put("pay_channel_id", pay_channel_id);
        map.put("sign", sign);
        logger.info(SYSTEMNAME+"请求支付参数："+map);
        
        //参数验证
        String errorMsg = validate(map);
        if(!"0000".equals(errorMsg)){
            return getErrorModel(errorMsg);
        }
        
        //取出商户其他信息
        Merchant merchant = merchantServices.byMerchant(mer_id);
        //查询商户编号是否存在
        if (merchant == null) {
            return getErrorModel("商户编号不存在");
        }

        String signKey = merchant.getMerchantSummaryKey();
        String privateKey = merchant.getMerchantPrivateDecrypt();
        Map<String,String> signMap = SignUtil.paraFilter(map);
        String str1 = SignUtil.createLinkString(signMap, "&");
        str1 = str1 + "&key=" + signKey;
        logger.debug(SYSTEMNAME+"验证签名串："+str1);
        String signStr = EncryptUtil.sha(str1);
        if (!signStr.equalsIgnoreCase(sign)) {
        	return getErrorModel("验证签名失败");
        }
		
        //解密商品信息
        String goodsStr = "";
        List<Map<String,String>> goodsMaps = new ArrayList<Map<String,String>>();
        if(StringUtil.isNotEmpty(goods_details)){
        	byte[] goodsData = null;
            try {
            	goodsData = EncryptUtil.dByPrivateKey(EncryptUtil.hex2b(goods_details), privateKey);
                if (goodsData == null){
                    return getErrorModel("商品明细不能解析");
                }
                goodsStr = new String(goodsData, "UTF-8");
                //  productStr用Json数据解析
                goodsMaps = JSONUtil.fromJSON(goodsStr, List.class);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return getErrorModel("商品明细解析异常");
            }
            
        }
        List<Map<String,String>> diagsMaps = new ArrayList<Map<String,String>>();
        //解密诊断信息
        if(StringUtil.isNotEmpty(diag_details)){
        	byte[] diagData = null;
        	String diagStr = "";
            try {
            	diagData = EncryptUtil.dByPrivateKey(EncryptUtil.hex2b(goods_details), privateKey);
                if (diagData == null){
                    return getErrorModel("诊断信息不能解析");
                }
                diagStr = new String(diagData, "UTF-8");
                //  productStr用Json数据解析
                diagsMaps = JSONUtil.fromJSON(diagStr, List.class);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return getErrorModel("诊断信息解析异常");
            }
            
        }
        //组装请求交易系统创建订单参数
        Map<String,Object> orderMap = new HashMap<String,Object>();
        orderMap.put("module_id","111111" );
        orderMap.put("version", "1");
        orderMap.put("merchant_order_id",out_order_id );
        orderMap.put("merchant_id", mer_id);
        orderMap.put("total_fee", StringUtil.yuan2Fen(order_amount)+"");
        orderMap.put("currency", "156");
        orderMap.put("content", "11111");
        orderMap.put("goods_count", goodsMaps.size()+"");
        if(goodsMaps!=null&&goodsMaps.size()>0){
        	orderMap.put("goods_detail", JSONUtil.toJSON(goodsMaps));
        }
        if(diagsMaps!=null&&diagsMaps.size()>0){
        	orderMap.put("diag_detail", JSONUtil.toJSON(diagsMaps));
        }
        orderMap.put("is_tip", "true");
        orderMap.put("buyer_name", "");
        orderMap.put("buyer_cellphone", "15111111111");
        orderMap.put("page_callback", page_url);
        orderMap.put("order_callback", bg_url);
        //TODO 签名
        orderMap.put("sign", "111111");
        logger.info(SYSTEMNAME+"请求交易系统创建订单请求参数:"+orderMap);
      //请求交易系统创建订单
        Map<String, Object> resultMap = HttpClient.getInstance().doPOSTgetHashMap("/transSystem/create_order",  orderMap);
        
      //取出map的值
  		if(resultMap == null || resultMap.size()<=0) {
            return getErrorModel("支付失败");
  		}
  		logger.info(SYSTEMNAME+"请求交易系统创建订单响应参数:"+resultMap);
  		String resp_status = (String)resultMap.get("resp_status");
  		String order_id = (String)resultMap.get("order_id");
  		if(!"0000".equalsIgnoreCase(resp_status)){
            return getErrorModel("支付失败");
  		}
  		if(StringUtil.isEmpty(order_id)){
  			return getErrorModel("支付失败");
  		}
  		//TODO 验签
  		String channelStr = (String)resultMap.get("channel");//获取返回渠道信息
  		if(StringUtil.isEmpty(channelStr)) {
  			errorMsg = "该商户没有配置支付渠道";
            return getErrorModel(errorMsg);
  		}
  		List<Map<String,String>> channels = JSONUtil.fromJSON(channelStr, List.class);
		if(channels==null||channels.size()<=0){
  			return getErrorModel("该商户没有配置支付渠道");
		}
		/*if(StringUtil.isNotEmpty(pay_channel_id)){
			boolean flag = false;
			for (Map<String, Object> channelMap : channels1) {
				if(pay_channel_id.equals(channelMap.get("id"))){
					flag = true;
					break;
				}
			}
			if(flag){
				ModelAndView model = new ModelAndView("error");
	  			model.addObject("errMsg", "该商户没有配置此支付渠道");
	  			return model;

			}
			
		}*/
		//组装支付渠道，去支付渠道选择页面
		if(StringUtil.isEmpty(pay_channel_id)){
			String[] channelType = pay_type.split(",");
			ModelAndView model = new ModelAndView("web/paygate");
			return model;
		}
		
		//直接去支付
		//如果是360积分支付则跳到360支付页面
		if("0004".equals(pay_channel_id)){
			//根据token查询用户信息请求账户系统查询用户信息接口
			Map<String,Object> reqInfoMap = new HashMap<String,Object>();
			
			//根据token查询用户信息
			reqInfoMap.put("token", user_id);
			Map<String, Object> repInfoMap = HttpClient.getInstance().doPOSTgetHashMap("/accmanage/query_user_info",reqInfoMap);
			if(repInfoMap==null||repInfoMap.size()<=0){
				return getErrorModel("查询用户信息失败");
			}
			String mobile = (String)repInfoMap.get("telephone");
//			String mobile = "15111111111";
			String userid = (String)repInfoMap.get("userid");
			
			
			
			ModelAndView model = new ModelAndView("jf/jifen");
	        model.addObject("local_pay_ssn", out_order_id);
	        model.addObject("order_id", order_id);
	        model.addObject("fee", order_amount);
	        model.addObject("channel_id", pay_channel_id);
	        model.addObject("buyer_token", user_id);
	        model.addObject("channel_type", "0003");
	        model.addObject("mobile", mobile);
	        
	        model.addObject("mer_id", mer_id);
	        model.addObject("input_charset", input_charset);
	        model.addObject("version", version);
	        model.addObject("language", language);
	        model.addObject("sign_type", sign_type);
	        model.addObject("userid", userid);
	        return model;
		}
		return null;
	}
	
	
	/**
	 * 账户、代扣支付
	 * 组装参数请求交易系统订单支付
	 * 将结果信息展示并返回给商户
	 * @param local_pay_ssn
	 * @param order_id
	 * @param fee
	 * @param channel_id
	 * @param buyer_token
	 * @param buyer_voucher
	 * @param channel_type
	 * @param mer_id
	 * @param input_charset
	 * @param version
	 * @param language
	 * @param sign_type
	 * @param sessionId
	 * @param userid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "doPay")
    public ModelAndView doPay( 
    		@RequestParam(required = false) String local_pay_ssn,
            @RequestParam(required = false) String order_id,
            @RequestParam(required = false) String fee,
            @RequestParam(required = false) String channel_id,
            @RequestParam(required = false) String buyer_token,
            @RequestParam(required = false) String buyer_voucher,
            @RequestParam(required = false) String channel_type,
            @RequestParam(required = false) String mer_id,
            @RequestParam(required = false) String input_charset,
            @RequestParam(required = false) String version,
            @RequestParam(required = false) String language,
            @RequestParam(required = false) String sign_type,
			@RequestParam(required = false) String sessionId,
			@RequestParam(required = false) String userid){
		 //订单支付
		Map<String,Object> payMap = new HashMap<String,Object>();
		payMap.put("module_id", "111111");
		payMap.put("version", "11");
		payMap.put("local_pay_ssn", local_pay_ssn);
		payMap.put("order_id", order_id);
		payMap.put("fee", StringUtil.yuan2Fen(fee));
		payMap.put("currency", "156");
		payMap.put("channel_id", channel_id);
		payMap.put("buyer_token", userid);//用户id
		payMap.put("buyer_voucher", buyer_voucher);//短信验证码
		payMap.put("channel_type", channel_type);
		
		
		//根据用户id和账户类型查询账户信息
		Map<String,Object> reqInfoMap = new HashMap<String,Object>();
		reqInfoMap.put("userId", userid);
		reqInfoMap.put("accType", "31");
		Map<String,Object> repInfoMap = HttpClient.getInstance().doPOSTgetHashMap("/accmanage/query_account",reqInfoMap);
		if(repInfoMap==null||repInfoMap.size()<=0){
			return getErrorModel("查询账户信息失败");
		}
		List<Map<String,String>> accounts = (List<Map<String,String>>)repInfoMap.get("accounts");
		if(accounts==null||accounts.size()<=0){
			return getErrorModel("查询账户信息失败");
		}
		String accId = accounts.get(0).get("accId");
		
		Map<String, Object> customMap = new HashMap<String, Object>();
		customMap.put("accId", accId); //账户id
		customMap.put("sessionId", sessionId);//短信会话id
		payMap.put("custom_str", JSONUtil.toJSON(customMap, HashMap.class));
		
		//TODO 签名
		payMap.put("sign", "111");
		logger.info(SYSTEMNAME+"请求交易系统订单支付请求参数："+payMap);
		//请求交易系统支付
		Map<String, Object> resultPayMap = HttpClient.getInstance().doPOSTgetHashMap("/transSystem/pay_order", payMap);
	    if(resultPayMap==null||resultPayMap.size()<=0){
	    	return getErrorModel("请求交易系统支付失败");
	    }
	    logger.info(SYSTEMNAME+"请求交易系统订单支付响应参数："+resultPayMap);
	    String resp_status = (String)resultPayMap.get("resp_status");
	    String sub_deal_ssn = (String)resultPayMap.get("sub_deal_ssn");
	    String sub_deal_stauts = (String)resultPayMap.get("sub_deal_stauts");
	    String sign = (String)resultPayMap.get("sign");
	    if(!"0000".equalsIgnoreCase(resp_status)){
             return getErrorModel("支付失败");
	    }
	    //TODO 验签
	    
	    Map<String,Object> reqMap = new HashMap<String,Object>();
		reqMap.put("module_id", "111111");
		reqMap.put("version", "v0.4");
		reqMap.put("sub_deal_ssn", sub_deal_ssn);
		reqMap.put("order_id", order_id);
		//TODO 签名
		reqMap.put("sign", "sign");
		Map<String, Object> repMap = HttpClient.getInstance().doPOSTgetHashMap("/transSystem/get_pay_status", reqMap);
		if(null==repMap||repMap.size()<=0){
			return getErrorModel("系统内部错误");
		}
		
		//跳转到商户通知页面
		//String pid = (String)repMap.get("mer_id");
		String page_url = (String)repMap.get("page_url");
	    
	    
        Map<String,String> responseMap = new HashMap<String,String>();
        responseMap.put("inputcharset",input_charset);
        responseMap.put("version",version);
        responseMap.put("language",language);
        responseMap.put("signtype",sign_type);
        responseMap.put("mer_id",mer_id);
        responseMap.put("user_id",(String)repMap.get("user_id"));
        responseMap.put("out_order_id",(String)repMap.get("out_order_id"));
        responseMap.put("order_amount",(String)repMap.get("order_amount"));
        responseMap.put("pay_amount",(String)repMap.get("pay_amount"));
        responseMap.put("fee",(String)repMap.get("fee"));
        responseMap.put("deal_id",(String)repMap.get("deal_id"));
        responseMap.put("bank_deal_id",(String)repMap.get("bank_deal_id"));
        responseMap.put("deal_time",(String)repMap.get("deal_time"));
        responseMap.put("pay_channel_name",(String)repMap.get("pay_channel_name"));
        responseMap.put("pay_type",(String)repMap.get("pay_type"));
        responseMap.put("pay_channel_id",(String)repMap.get("pay_channel_id"));
        responseMap.put("ext1", (String)repMap.get("ext1"));
        responseMap.put("ext2", (String)repMap.get("ext2"));
        //支付结果
        if("1001".equals((String)repMap.get("pay_status"))){
        	responseMap.put("pay_status", "10");
        }else{
        	responseMap.put("pay_status", "11");
        }
      //取出商户其他信息
        Merchant merchant = merchantServices.byMerchant(mer_id);
        //查询商户编号是否存在
        if (merchant == null) {
            return getErrorModel("商户编号不存在");
        }
        String signKey = merchant.getMerchantSummaryKey();
       //String privateKey = merchant.getMerchantPrivateDecrypt();
        String str1 = SignUtil.createLinkString(SignUtil.paraFilter(responseMap),"&");
        str1 = str1 + "&key=" + signKey;
        logger.debug(SYSTEMNAME+"支付通知商户待签名串："+str1);
        String signStr = EncryptUtil.sha(str1);
        responseMap.put("sign", signStr);
        
        
        ModelAndView model = new ModelAndView("web/tomer");
        model.addObject("responseMap",responseMap);
        logger.debug(SYSTEMNAME+"支付通知商户参数"+responseMap);
        model.addObject("page_url", page_url);
        return model;
	}
	
	
	/**
	 * 组装参数请求交易系统订单支付接口
	 * 订单处理成功则组装银行请求参数返回
	 * @param input_charset
	 * @param version
	 * @param language
	 * @param sign_type
	 * @param mer_id
	 * @param user_id
	 * @param page_url
	 * @param bg_url
	 * @param out_order_id
	 * @param order_amount
	 * @param goods_details
	 * @param diag_details
	 * @param ext1
	 * @param ext2
	 * @param pay_type
	 * @param pay_channel_id
	 * @param order_id
	 * @return
	 */
	@RequestMapping(value = "doGatePay")
    public ModelAndView doGatePay(
    		@RequestParam(required = false) String input_charset,
            @RequestParam(required = false) String version,
            @RequestParam(required = false) String language,
            @RequestParam(required = false) String sign_type,
            @RequestParam(required = false) String mer_id,
            @RequestParam(required = false) String user_id,
            @RequestParam(required = false) String page_url,
            @RequestParam(required = false) String bg_url,
            @RequestParam(required = false) String out_order_id,
            @RequestParam(required = false) String order_amount,
            @RequestParam(required = false) String goods_details,
            @RequestParam(required = false) String diag_details,
            @RequestParam(required = false) String ext1,
            @RequestParam(required = false) String ext2,
            @RequestParam(required = false) String pay_type,
            @RequestParam(required = false) String pay_channel_id,
			@RequestParam(required = false) String order_id){
		//组装请求订单支付参数
		Map<String,Object> payMap = new HashMap<String,Object>();
		payMap.put("module_id", "111111");
		payMap.put("version", "11");
		payMap.put("sign", "111");
		payMap.put("local_pay_ssn", "11");
		payMap.put("order_id", order_id);
		payMap.put("fee", StringUtil.yuan2Fen(order_amount));
		payMap.put("currency", "156");
		payMap.put("channel_id", pay_channel_id);
		payMap.put("buyer_token", "11");
		payMap.put("buyer_voucher", "11");
		payMap.put("channel_type", pay_type);
		
		logger.info(SYSTEMNAME+"请求交易系统订单支付接口请求参数："+payMap);
		
		//请求交易系统支付
		Map<String, Object> resultPayMap = HttpClient.getInstance().doPOSTgetHashMap("/transSystem/pay_order", (HashMap<String, Object>) payMap);
	    
		if(resultPayMap==null||resultPayMap.size()<=0){
			getErrorModel("请求交易系统订单支付接口失败");
		}
	    String resp_status = (String)resultPayMap.get("resp_status");
	    String sub_deal_ssn = (String)resultPayMap.get("sub_deal_ssn");
	    String sub_deal_stauts = (String)resultPayMap.get("sub_deal_stauts");
	    String sign = (String)resultPayMap.get("sign");
	    if(!"0000".equalsIgnoreCase(resp_status)){
             return getErrorModel("支付失败");
	    }
	    //TODO 验签
        //调用银行加密方法组装银行参数
	    ModelAndView model = new ModelAndView("web/tobank");
        return model;
	}
	
	
	
	/**
	 * 发送短信验证码(请求账户系统发送短信验证码)
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "getSMSVerifCode")
	public void getSMSVerifCode( HttpServletRequest request, HttpServletResponse response){
		String telephone = request.getParameter("telephone");
		logger.debug(SYSTEMNAME+"短信验证码发送号码"+telephone);
		Map<String,String> map = new HashMap<String,String>();
		try {
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8");
			
			if (StringUtil.isEmpty(telephone)) {
				map.put("status", "1002");//号码为空
				response.getWriter().print(JSONUtil.toJSON(map));
				return;
			}
			
			Map<String, Object> reqSmsMap = new HashMap<String, Object>();
			reqSmsMap.put("telephone", telephone);
			reqSmsMap.put("sign", "");
			Map<String, Object> repSmsMap = HttpClient.getInstance().doPOSTgetHashMap("/accmanage/send_sms", (HashMap<String, Object>) reqSmsMap);
			if (repSmsMap != null) {
				String status = (String)repSmsMap.get("status");
				if("0001".equals(status)){
					map.put("status", "0000");//发送成功
					map.put("sessionId", (String)repSmsMap.get("session"));
					response.getWriter().print(JSONUtil.toJSON(map));
					return;
				}
			}
			map.put("status", "1001");//发送失败
			response.getWriter().print(JSONUtil.toJSON(map));
		} catch (Exception e) {
			logger.error("发送短信验证码错误：" + telephone + e);
		}
		return;
	}
	
	/**
	 * 请求参数校验
	 * @param map
	 * @return 0000 表示参数验证通过
	 */
	public String validate(Map<String,String> map){
		String errorMsg = "0000";
		if(map==null){
			errorMsg = "请求参数为空";
			return errorMsg;
		}
		//参数验证
        if (!"1".equalsIgnoreCase(map.get("input_charset"))) {
            errorMsg = "协议错误";
            return errorMsg;
        }

        if (!"v1.0".equalsIgnoreCase(map.get("version"))) {
            errorMsg = "协议错误";
            return errorMsg;
        }

        if (!"1".equalsIgnoreCase(map.get("language"))) {
            errorMsg = "协议错误";
            return errorMsg;
        }
        if (StringUtil.isEmpty(map.get("sign"))) {
            errorMsg = "签名字段为空";
            return errorMsg;
        }
        if (StringUtil.isEmpty(map.get("mer_id"))) {
        	 errorMsg = "商户号为空";
             return errorMsg;
        }
//        if (StringUtil.isEmpty(user_id)) {
//        	ModelAndView model = new ModelAndView("error");
//        	model.addObject("errMsg", "");
//        	return model;
//        }
        if (StringUtil.isEmpty(map.get("page_url"))) {
            errorMsg = "无引用地址，无法返回商户页面";
            return errorMsg;
        }
        if (!StringUtil.isTradeId(map.get("out_order_id"))||map.get("out_order_id").length()>40) {
        	errorMsg = "商户订单号格式不正确";
            return errorMsg;
        }
        if (!StringUtil.isAmount(map.get("order_amount"))) {
        	errorMsg = "订单金额格式不正确";
            return errorMsg;
        }
        /*if (!StringUtil.IsDateTimeString(map.get("order_time"))) {
        	errorMsg = "商户订单提交时间为空或者格式不正确";
            return errorMsg;
        }*/
        if (StringUtil.isNotEmpty(map.get("ext1"))&&map.get("ext1").length()>200) {
        	errorMsg = "扩展字段1过长";
            return errorMsg;
        }
        if (StringUtil.isNotEmpty(map.get("ext2"))&&map.get("ext2").length()>200) {
        	errorMsg = "扩展字段2过长";
            return errorMsg;
        }
        if (StringUtil.isEmpty(map.get("pay_type"))) {
        	errorMsg = "支付渠道类型为空";
            return errorMsg;
        }
        if (StringUtil.isEmpty(map.get("sign"))) {
        	errorMsg = "签名为空";
        	return errorMsg;
        }
		return errorMsg;
	}
	
	/**
	 * 根据错误消息获取model
	 * @param errorMsg
	 * @return
	 */
	public ModelAndView getErrorModel(String errorMsg){
		logger.error(SYSTEMNAME+errorMsg);
		ModelAndView model = new ModelAndView("error");
		model.addObject("errMsg", errorMsg);
		return model;
		
	}
	public static void main(String[] args) {
//		Map<String, Object> reqSmsMap = new HashMap<String, Object>();
//		reqSmsMap.put("telephone", "15101519251");
//		reqSmsMap.put("sign", "11");
//		Map<String, Object> repSmsMap = HttpClient.getInstance().doPOSTgetHashMap("/accmanage/send_sms", (HashMap<String, Object>) reqSmsMap);
		
//		Map<String,Object> reqInfoMap = new HashMap<String,Object>();
//		reqInfoMap.put("token", "2z3Awwb1XxWCcT4jYf5jYY6n94fWYafSM2BkSfLRYQM7dkW1XuuwUFUrEdhhVPJD21km5kK2nkQ5WYS8jnU1a52b");
//		Map<String, Object> repInfoMap = HttpClient.getInstance().doPOSTgetHashMap("/accmanage/query_user_info",reqInfoMap);
//		System.out.println(repInfoMap);
//		Map<String,Object> reqInfoMap = new HashMap<String,Object>();
//		reqInfoMap.put("appid", "wxcec8863ae602c67c");
//		reqInfoMap.put("secret", "701f0114d3ee3ebf972f162bafbc42f9");
//		reqInfoMap.put("code", "02cdfae887471a31c4ce87d868dd024c");
//		reqInfoMap.put("grant_type", "authorization_code");
//		//https://api.weixin.qq.com/sns/oauth2/access_token
//		Map<String, Object> repInfoMap = HttpClient.getInstance().doPOSTgetHashMap("https://api.weixin.qq.com/sns/oauth2/access_token",reqInfoMap);
//		System.out.println(repInfoMap);
		//<a href="https://open.weixin.qq.com/connect/oauth2/authorize?
//		appid=wxcec8863ae602c67c&
//				redirect_uri=http://payment-test.jiankangjin.com/jkj-paygate/page/keyTest.action&
//					response_type=code&
//					scope=snsapi_userinfo&
//					state=123#wechat_redirect">
//		Map<String,Object> reqInfoMap = new HashMap<String,Object>();
//		reqInfoMap.put("appid", "wxcec8863ae602c67c");
//		reqInfoMap.put("redirect_uri", "http://payment-test.jiankangjin.com/jkj-paygate/page/keyTest.action");
//		reqInfoMap.put("response_type", "code");
//		reqInfoMap.put("scope", "snsapi_userinfo");
//		reqInfoMap.put("state", "123");
		//https://api.weixin.qq.com/sns/oauth2/access_token
//		Map<String, Object> repInfoMap = HttpClient.getInstance().doPOSTgetHashMap("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxcec8863ae602c67c&redirect_uri=http://payment-test.jiankangjin.com/jkj-paygate/page/keyTest.action&response_type=code&scope=snsapi_base&state=123#wechat_redirect",null);
		//System.out.println(repInfoMap);
		
//		Map<String,Object> reqInfoMap = new HashMap<String,Object>();
//		reqInfoMap.put("appid", "wxcec8863ae602c67c");
//		reqInfoMap.put("secret", "701f0114d3ee3ebf972f162bafbc42f9");
//		reqInfoMap.put("code", "02125ef6e0e1f77785a8ba6930eb9e19");
//		reqInfoMap.put("grant_type", "authorization_code");
//		Map<String, Object> repInfoMap = HttpClient.getInstance().doPOSTgetHashMap("https://api.weixin.qq.com/sns/oauth2/access_token",reqInfoMap);
//		logger.info("请求微信获取openid返回参数："+repInfoMap);
//		String openid = (String)repInfoMap.get("openid");
//		System.out.println(openid);
		
		try {
			String redirect_url =  URLEncoder.encode("http://payment-test.jiankangjin.com/jkj-paygate/wx/doPay?order_id=20140926174936131-000001GqXXUBw8&orderamount=0.01&channelid=0013&channeltype=WXPUB&spbill_create_ip=127.0.0.1", "utf-8");
		
		System.out.println(redirect_url);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
}
